package cn.edu.cdp.dao;

import cn.edu.cdp.domain.Major;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface MajorDao {
    @Select("select m.major_id majorId, m.name, m.category, c.name collegeName " +
            "from major m left join college c on m.college_id=c.college_id")
    List<Map<String, Object>> allMajor();


    @Select("select m.college_id, m.name, m.category, m.college_id " +
            "from major m, college c where m.college_id=c.college_id and c.name=#{collegeName}")
    List<Major> filterMajorByCollegeName(String collegeName);

    @Select("select major_id from major where name=#{majorName}")
    String getIdByName(String majorName);

    @Insert("insert into major (major_id, name, category, college_id) values (#{majorId}, #{name}, #{category}, #{collegeId})")
    void insert(Major major);

    @Update("update major set name=#{name}, category=#{category}, college_id=#{collegeId} where major_id=#{majorId}")
    void update(Major major);

    @Delete("delete from major where major_id=#{majorId}")
    void deleteById(String majorId);
}
